package com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.BankCards;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.model.bean.AddProceedBean;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.ReceiptInformationActivity;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.Bind;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.OkHttpFinal;
import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;

public class NewBankCardsActivity extends BaseActivity {

    @Bind(R.id.finish_img)
    ImageButton mIb_back;
    @Bind(R.id.tv_bank_open)
    TextView tv_bank_open;
    @Bind(R.id.view_title)
    TextView mV_title;
    @Bind(R.id.ed_bankcards_name)
    EditText ed_bankcards_name;
    @Bind(R.id.ed_bankcards_number)
    EditText ed_bankcards_number;
    @Bind(R.id.rl_select_bank)
    RelativeLayout rl_select_bank;
    @Bind(R.id.bt_finish)
    Button bt_finish;

    private RequestParams params;

    @Override
    public void init() {
        setStatuColor();

        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.NewBank);

        //选择银行之后回来填充数据
        tv_bank_open.setText(getIntent().getStringExtra("tv_bank_open"));
        ed_bankcards_name.setText(SharedPrefsUtil.getValue(NewBankCardsActivity.this, "wallet", "new_bank_name",ed_bankcards_name.getText().toString() ));
        ed_bankcards_number.setText(SharedPrefsUtil.getValue(NewBankCardsActivity.this, "wallet", "new_bank_number", ed_bankcards_number.getText().toString()));
        if ("".equals(tv_bank_open.getText().toString())){
            tv_bank_open.setText(R.string.BankCardOpening);
        }


        rl_select_bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPrefsUtil.putValue(NewBankCardsActivity.this, "wallet", "new_bank_name",ed_bankcards_name.getText().toString() );
                SharedPrefsUtil.putValue(NewBankCardsActivity.this, "wallet", "new_bank_number", ed_bankcards_number.getText().toString());
                Intent i = new Intent(getApplicationContext(), SelectBankActivity.class);
                i.putExtra("context","NewBankCardsActivity");
                startActivity(i);
//                finish();
                overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
            }
        });
        bt_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
                OkHttpFinal.getInstance().init(builder.build());
                params = new RequestParams();
                params.addFormDataPart("access_token", SharedPrefsUtil.getValue(NewBankCardsActivity.this, "wallet", "token", ""));
                params.addFormDataPart("proceeds_type","bank");
                params.addFormDataPart("account",ed_bankcards_number.getText().toString());
                params.addFormDataPart("username",ed_bankcards_name.getText().toString());
                params.addFormDataPart("bank_name",tv_bank_open.getText().toString());//tv_bank_open.getText().toString();   "银行卡开户行".equals(tv_bank_open.getText().toString())
                if ("".equals(ed_bankcards_number.getText().toString()) | "".equals(ed_bankcards_name.getText().toString()) | "银行卡开户行".equals(tv_bank_open.getText().toString())){
                    Toast.makeText(getApplicationContext(),R.string.Submissioncannotbeempty,Toast.LENGTH_SHORT).show();
                }else{
                    //银行卡信息上传
                    HttpRequest.post("https://wallet.kinlink.cn/api/gathering/add-proceed",params,new BaseHttpRequestCallback<AddProceedBean>(){
                        @Override
                        protected void onSuccess(Headers headers, AddProceedBean addProceedBean) {
                            super.onSuccess(headers, addProceedBean);
                            int code = addProceedBean.getCode();
                            if (code == 200){
                                Toast.makeText(getApplicationContext(),addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                                onBackPressed();
                            }else {
                                Toast.makeText(getApplicationContext(),addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(int errorCode, String msg) {
                            super.onFailure(errorCode, msg);

                        }
                    });
                }
            }
        });

        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    @Override
    public int getLayoutResource() {
        return R.layout.activity_new_bank_cards;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 100:

                break;
        }
    }

    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(getApplicationContext(),ReceiptInformationActivity.class));
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

}
